from alipay import AliPay, AliPayConfig
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View
from apps.orders.models import OrderInfo
from meiduo_mall import settings

'''
买家账号mjwwnm0084@sandbox.com
登录密码111111
支付密码111111
'''
class PayUrlView(LoginRequiredMixin, View):
    def get(self, request, order_id):
        try:
            order = OrderInfo.objects.get(order_id=order_id,
                                          status=OrderInfo.ORDER_STATUS_ENUM['UNPAID'])
        except OrderInfo.DoesNotExist:
            return JsonResponse({'code': 400, 'errmsg': '订单信息有误'})

        app_private_key_string = open(settings.ALIPAY_PRIVATE_KEY_PATH).read()
        alipay_public_key_string = open(settings.ALIPAY_PUBLIC_KEY_PATH).read()

        alipay = AliPay(
            appid = settings.ALIPAY_APPID,
            app_notify_url = None,  # 默认回调url
            app_private_key_string = app_private_key_string,
            alipay_public_key_string = alipay_public_key_string,
            sign_type = 'RSA2',
            debug = settings.ALIPAY_DEBUG,
            config = AliPayConfig(timeout=15)
        )

        subject = '美多商城%s' % order_id
        order_string = alipay.api_alipay_trade_page_pay(
            out_trade_no = order_id,
            total_amount = str(order.total_amount),
            subject = subject,
            return_url = settings.ALIPAY_RETURN_URL,
            notify_url = None,  # 可选, 不填则使用默认notify url
            product_code = 'FAST_INSTANT_TRADE_PAY'
        )

        pay_url = 'https://openapi-sandbox.dl.alipaydev.com/gateway.do?'+order_string
        return JsonResponse({'code': 0, 'errmsg': 'OK', 'alipay_url': pay_url})

class PaymentStatusView(View):
    def put(self, request):
        data = request.GET
        signature = data.pop('sign')

        app_private_key_string = open(settings.ALIPAY_PRIVATE_KEY_PATH).read()
        alipay_public_key_string = open(settings.ALIPAY_PUBLIC_KEY_PATH).read()

        alipay = AliPay(
            appid=settings.ALIPAY_APPID,
            app_notify_url=None,  # 默认回调url
            app_private_key_string=app_private_key_string,
            alipay_public_key_string=alipay_public_key_string,
            sign_type='RSA2',
            debug=settings.ALIPAY_DEBUG,
            config=AliPayConfig(timeout=15)
        )

        success = alipay.verify(data,signature)
        if success:
            trade_no = data.get('trade_no')
            order_id = data.get('out_trade_no')
            Payment = OrderInfo.objects.create(
                trade_id=trade_no,
                order_id=order_id,
            )
            OrderInfo.objects.filter(order_id=order_id).update(status=OrderInfo.ORDER_STATUS_ENUM['UNSEND'])
            return JsonResponse({'code': 0, 'errmsg': 'OK', 'trade_id': trade_no})
        else:
            return JsonResponse({'code': 400, 'errmsg': '非法请求'})